<template>
  <!-- 添加或修改记账项目对话框 -->
  <el-dialog :title="pd.title" v-model="pd.open" width="700px" append-to-body>
    <el-form ref="classificationItemRef" :model="pd.form" :rules="pd.rules" label-width="80px">
      <el-row>
        <el-col :span="20">
          <el-form-item label="名称" prop="name" label-width="100px">
            <el-input v-model="pd.form.name" placeholder="请选择名称"/>
          </el-form-item>
          <el-form-item label="记账科目编码" prop="classificationCode" label-width="100px">
            <ClassificationSearch :title="classificationSearchParam.title" :isSingle="classificationSearchParam.isSingle"
                           @selectd="classificationSelectd"></ClassificationSearch>
          </el-form-item>
          <el-form-item label="租户" prop="tenantId" label-width="100px">
            <el-input v-model="pd.form.tenantId" placeholder="请输入租户"/>
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="pd.open = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="ClassificationItemCurd">
import ClassificationSearch from "@/views/fms/classification/classificationSearch"
import {delClassificationItem, addClassificationItem, updateClassificationItem} from "@/api/fms/classificationItem";
import {getCurrentInstance, reactive} from "vue";
const {proxy} = getCurrentInstance();
const {hrp_user_sex} = proxy.useDict('hrp_user_sex');
let pd = reactive({
  open: false,
  title: "",
  operation: 0,
  form: {
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code:null,
    name: null,
    accountType: null,
    internalGroup: null,
    tenantId: null,
    createBy: null,
    createTime: null,
  },
  rules: {
    name: [{
      required: true,
      message: "名称不能为空",
      trigger: "change"
    }],
    tenantId: [{
      required: true,
      message: "租户不能为空",
      trigger: "change"
    }]
  }
});

const {
  form,
  queryParams,
  rules
} = toRefs(pd);

const classificationSearchParam = reactive({
  title: "记账科目查找",
  isSingle: true,
});

function classificationSelectd(searchReParams) {
  searchReParams.forEach(function (item) {
    console.log(item);
    pd.form.classificationCode=item.code
  });
}


/** 提交按钮 */
function submitForm() {
  if (pd.operation == 1){
    addClassificationItem(pd.form).then(response => {
      proxy.$modal.msgSuccess("新增成功");
    });
  } else if (pd.operation == 2){
    updateClassificationItem(pd.form).then(response => {
      proxy.$modal.msgSuccess("修改成功");
    });
  }
  pd.open = false;
  emit("ok", pd.operation);
}

// 新增
function add(classificationItem) {
  reset();
  //合并
  pd.form = {...pd.form , ...classificationItem};
  pd.title = "新增记账项目";
  pd.operation = 1;
  pd.open = true;
}
// update
function update(classificationItem) {
  reset();
  //合并
  pd.form = {...pd.form , ...classificationItem};
  pd.title = "修改记账项目";
  pd.operation = 2;
  pd.open = true;
}
// del
function del(ids) {
  pd.operation = 3;
  proxy.$modal.confirm('是否确认删除记账项目编号为"' + ids + '"的数据项？').then(function () {
    return delClassificationItem(ids);
  }).then(() => {
    proxy.$modal.msgSuccess("删除成功");
    emit("ok", pd.operation);
  }).catch(() => {
  });
}

// 表单重置
function reset() {
  pd.form = {
    name: null,
    classificationCode:null,
    tenantId: null,
    createBy: null,
    createTime: null,
  };
  pd.operation = 0;
  pd.title = "";
  proxy.resetForm("classificationItemRef");
}
defineExpose({ add,update,del });
const emit = defineEmits(["ok"]);

</script>
